package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private StrategyTagMapper strategyTagMapper;

    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    /**
     * 攻略文章管理的分页
     * @param qo
     * @return
     */
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据id查询文章内容
     * @param id
     * @return
     */
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(StrategyDetail entity, String tags) {
        //获取文章内容
        StrategyContent content = entity.getStrategyContent();

        //设置目的地
        Long catalogId = entity.getCatalog().getId();
        StrategyCatalog catalog = strategyCatalogMapper.selectByPrimaryKey(catalogId);
        //创建目的地对象并保存
        Destination dest = new Destination();
        dest.setId(catalog.getDestId());
        entity.setDest(dest);

        //设置摘要
        entity.setSummary("AI载入中.....");
        if (entity.getId() == null){

            //设置创建时间
            entity.setCreateTime(new Date());
            //保存文章
            strategyDetailMapper.insert(entity);
            //保存文章内容
            content.setId(entity.getId());
            strategyContentMapper.insert(content);
        }else {
            strategyDetailMapper.updateByPrimaryKey(entity);
            strategyContentMapper.updateByPrimaryKey(content);
        }

        //处理标签
        if (tags != null) {
            //注意:需要先删除原来标签和文章的关系
            strategyDetailMapper.deleteRelation(entity.getId());
            String[] ts = tags.split(",");
            for (String tagName : ts) {
                //查询标签的名字
                StrategyTag tag = strategyTagMapper.selectByName(tagName);
                //标签不存在,则保存标签数据到数据库中
                if (tag == null) {
                    tag = new StrategyTag();
                    strategyTagMapper.insert(tag);
                }
                //当前标签和当前文章的关系
                strategyDetailMapper.insertRelation(entity.getId(),tag.getId());
            }
        }
    }

    /**
     * 查询分类下点击量前3的文章
     * @param id
     * @return
     */
    public List<StrategyDetail> listViewnumTop3(Long id) {
        return strategyDetailMapper.selectviewnumTop3(id);
    }

    /**
     * 查询文章相关的所有数据
     * @return
     */
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    /**
     * 更新文章的统计数据
     * @param detail
     */
    public void updateStats(StrategyDetail detail) {
        strategyDetailMapper.updateStats(detail);
    }

}
